package run.siyuan.config;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @className: MemcachedBuilder
 * @Description: TODO
 * @author: siyuan
 * @date: 2022/10/31 16:49
 */

@Configuration
public class MemcachedBuilder {

    @Autowired
    private Environment environment;

    @Bean
    public MemcachedClient memcachedClient(){
        try {
            XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(environment.getProperty("memcached.servers")));
            builder.setConnectionPoolSize(Integer.valueOf(environment.getProperty("memcached.poolSize")));
            builder.setOpTimeout(Integer.valueOf(environment.getProperty("memcached.opTimeout")));
            return builder.build();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
